package lk.icta.core.dao.impl;

import lk.icta.common.AppConstants;
import lk.icta.common.dao.PreloadableDAO;
import lk.icta.common.dao.impl.BaseDAO;
import lk.icta.core.dao.LocalAuthorityTypeDAO;
import lk.icta.core.entity.LocalAuthorityType;
import lk.icta.common.ErrorCodes;
import lk.icta.user.entity.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Duminda Dharmakeerthi
 */
public class LocalAuthorityTypeDAOImpl extends BaseDAO implements LocalAuthorityTypeDAO, PreloadableDAO {

    private final Map<Integer, String> siNames = new HashMap<Integer, String>();
    private final Map<Integer, String> enNames = new HashMap<Integer, String>();
    private final Map<Integer, String> taNames = new HashMap<Integer, String>();

    @Transactional(propagation = Propagation.MANDATORY)
    public void addLocalAuthority(LocalAuthorityType localAuthorityType, User user) {
        logger.debug("Adding a Local Authority {}", localAuthorityType.getEnName());
        em.persist(localAuthorityType);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void updateLocalAuthority(LocalAuthorityType localAuthorityType, User user) {
        logger.debug("Updating a Local Authority {}", localAuthorityType.getEnName());
        em.merge(localAuthorityType);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public LocalAuthorityType getLocalAuthorityById(int id, User user) {
        logger.debug("Find a Local Authority by Id: {}", id);
        return em.find(LocalAuthorityType.class, id);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<LocalAuthorityType> getAllLocalAuthorities() {
        logger.debug("List all Local Authorities");
        Query q = em.createNamedQuery("getAllLocalAuthorities");
        return q.getResultList();
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public Map<Integer, String> getAllLocalAuthorities(String language, User user) {
        logger.debug("Listing Local Authorities in {}", language);
        Map<Integer, String> result = null;
        if (AppConstants.SINHALA.equals(language)) {
            result = siNames;
        } else if (AppConstants.ENGLISH.equals(language)) {
            result = enNames;
        } else if (AppConstants.TAMIL.equals(language)) {
            result = taNames;
        } else {
            handleException("Unsupported language : " + language, ErrorCodes.INVALID_LANGUAGE);
        }
        if (result != null)
            return result;
        else
            return Collections.emptyMap();
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public void preload() {
        Query query = em.createQuery("SELECT l FROM LocalAuthorityType l");
        List<LocalAuthorityType> results = query.getResultList();
        for (LocalAuthorityType l : results) {
            siNames.put(l.getId(), l.getSiName());
            enNames.put(l.getId(), l.getEnName());
            taNames.put(l.getId(), l.getTaName());
        }
    }
}
